<?php
/*
Primary Author: Derek Shue
Secondary: Sean Margison (primary for coursedetails)
Updated: 12/11/2011
Module Description: The Teacher controller is responsible for teacher actions including
submitting courses and viewing the volunteer positions associated to the courses.
*/
class TeacherController extends Zend_Controller_Action
{
	/**
	* Init checks user session and type exists and redirects otherwise.
	*/
    public function init()
    {
        $auth = Zend_Auth::getInstance(); 
		if($auth->hasIdentity()){
			$type = $auth->getIdentity()->getUserType();
			if($type!='teacher'){
				throw new Exception("Error, page not found");
				return;
			}
			
		}else{
			$this->_helper->redirector('index','index');
			return;
		}
    }

    public function indexAction()
    {
    	return $this->_helper->redirector('mycoursesAction');
	}
	
	/**
	* Displays courses for the current term and specified teacher id
	*/
	public function mycoursesAction(){
		$auth = Zend_Auth::getInstance(); 
		if($auth->hasIdentity()){
			$teacherID = $auth->getIdentity()->getUID();
		}else{
			$this->_helper->redirector('index','index');
			return;
		}
		$courseQuery = new Application_Model_CoursesQuery();
		$courses = $courseQuery->getCourses_Teacher($teacherID);
		$this->view->courses = $courses;	
    }
    
	/**
	* Browse courses related to the teacher. Shows courses matching query form.
	*/
	public function browsecoursesAction(){

		$auth = Zend_Auth::getInstance(); 
		if($auth->hasIdentity()){
			$teacherID = $auth->getIdentity()->getUID();
		}else{
			$this->_helper->redirector('index','index');
			return;
		}
		
		$form = new Application_Form_CourseSearch();
		$this->view->form = $form;

		$courseQuery = new Application_Model_CoursesQuery();
		//set defaults
		$termModifier = 'ns';
		$term = '';
		$coursename = '';
		$coursenumber = 0;
		$date = new Zend_Date();
				
		//overwrite defaults with form data
		if ($this->getRequest()->isPost()) {
		        $formData = $this->getRequest()->getPost();
		        if ($form->isValid($formData)) {
		        	if($form->getValue('YEAR') == ""){
		        		$term = array('term'=>$form->getValue('TERM') , 'year' => $date->get(Zend_Date::YEAR));
		        	}else{
		        		$term = array('term'=>$form->getValue('TERM') , 'year' => $form->getValue('YEAR'));
		        	}
		            $termmodifier = $form->getValue('MODIFIER');
					$department = $form->getValue('DEPARTMENT');
					$coursenumber = $form->getValue('COURSENUMBER');
					$courses = $courseQuery->selectCourses_Teacher($teacherID, $term, $termmodifier, $department, $coursenumber);
			    }
			}
		else{
				$courses = $courseQuery->getCourses_Teacher($teacherID);
			}
			$this->view->entries = $courses;
    }

    

	/**
	* Handles course submit form and processing.
	*/
	public function submitcourseAction(){
		$auth = Zend_Auth::getInstance(); 
		if($auth->hasIdentity()){
			$teacherID = $auth->getIdentity()->getUID();
		}else{
			$this->_helper->redirector('index','index');
			return;
		}
		
		$form = new Application_Form_AddCourse();
        $form->submit->setLabel('Add Course');
        $term = new Application_Model_Term();
		$courses = new Application_Model_DbTable_Courses();
		$currTerm = $term->getCurrentOpenTermStr('facCourse');
		if(!$currTerm){
			$this->view->noTerm = true;
			return;
		}
		$this->view->term = $currTerm;
		$this->view->form = $form;
		
		 if ($this->getRequest()->isPost()) {
		 	$formData = $this->getRequest()->getPost();
            if ($form->isValid($formData)) {
				
            	$coursenumber = $form->getValue('COURSENUMBER');
				$department = $form->getValue('DEPARTMENT');
				$description = $form->getValue('DESCRIPTION');
				$coursename = $form->getValue('COURSENAME');	
				$check_syllabus = $form->getValue('SYLLABUS');
				if(substr($check_syllabus,0,7)=="http://"){//add http:// if not already present; needed to display link correctly
					$syllabus = $form->getValue('SYLLABUS');
				}else{
					$syllabus = "http://".$check_syllabus;
				}

					try{
						$courses->addCourse($coursenumber,$coursename,$description, $department, $teacherID, $currTerm, $syllabus);
					}
					catch(Exception $e){
						echo $e->getMessage();
						return null;
					}
				$this->_helper->redirector('mycourses');
			}
		 }
	}

	/**
	*	Delete the specified course.
	*/
	public function deletecourseAction() {
		if ($this -> getRequest() -> isPost()) {
			$del = $this -> getRequest() -> getPost('del');
			if ($del == 'Yes') {
				$cid = $this -> _getParam('CID');
				$courses = new Application_Model_DbTable_Courses();
				$courses->deleteCourse('CID = '.$cid);
			}

			$this -> _helper -> redirector('mycourses');
		} else {
			$cid = $this -> _getParam('CID');
			$courses = new Application_Model_DbTable_Courses();
			$this -> view -> course = $courses -> getCourse('CID = ' . $cid);
		}
	}
	/**
	* Display details for the sepcified course in an editable form. Allows course updates.
	*/
	public function coursedetailsAction(){
		$form = new Application_Form_AddCourse();
		
		$form->submit->setLabel('Save');
		$this->view->form = $form;

		$auth = Zend_Auth::getInstance();
		$teacherID = $auth -> getIdentity() -> getUID();
		$cid = $this -> _getParam('courseid');
		
		if ($this->getRequest()->isPost()) {
            $formData = $this->getRequest()->getPost();
            if ($form->isValid($formData)) {
				$coursenumber = $form->getValue('COURSENUMBER');
				$coursename = $form->getValue('COURSENAME');
				$description = $form->getValue('DESCRIPTION');
				$department = $form->getValue('DEPARTMENT');
				$term = $form->getValue('TERM'); 
				$syllabus = $form->getValue('SYLLABUS');
				$courses = new Application_Model_DbTable_Courses();
				$courses->updateCourse($cid,$coursenumber,$coursename,$description,$department,$teacherID,$term,$syllabus);
				$this->_helper->redirector('mycourses');
			} else{
				$form->populate($formData);
			}
		} else {
			if($cid > 0){
				$courses = new Application_Model_DbTable_Courses();
				$form->populate($courses->getCourse('CID = '.$cid));
			}
		}
	}
}






